package liveDemo.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import liveDemo.model.LiveRedis;
import liveDemo.model.UserRedis;
import liveDemo.service.LiveService;
import liveDemo.service.UserService;

@RestController
@EnableAutoConfiguration
public class RegisterController {
	@Autowired
	UserService userService;
	@Autowired
	LiveService liveService;
	
	@RequestMapping(value = "/reg" , method = RequestMethod.GET)
	public ModelAndView toRegister(ModelMap model){
		UserRedis user = new UserRedis();
		model.addAttribute("user", user);
		return new ModelAndView("register");
	}
	
	//@ModelAttribute(value = "user") UserRedis user注释方法参数，参数user的值来源于model属性。
	@RequestMapping(value = "/register" , method = RequestMethod.POST)
	public ModelAndView register(@ModelAttribute(value = "user") UserRedis user , HttpSession session , ModelMap model){
		userService.save(user);
		session.setAttribute("user", user);	
		List<LiveRedis> liveList = liveService.getAll();
		model.addAttribute("liveList", liveList);
		return new ModelAndView("liveList").addObject("message", "恭喜："+user.getName()+"用户注册成功！-点击“确定”进入主页面");
	}
	@RequestMapping(value = "/checkEmail")
	@ResponseBody
	public boolean checkEmail(String email){
		System.out.println("我能拿到值"+email);
		if(!email.isEmpty()){
			UserRedis user = userService.getByKey(email);			
			if(user != null){
				System.out.println("已经存在该哟农户");
				return false;
				
			}
		}
		return true;
	}

}
